unit WndMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, Menus, ExtCtrls;

type
  TWndViewerMain = class(TForm)
    MainMenu: TMainMenu;
    File1: TMenuItem;
    VBar: TScrollBar;
    StatusBar1: TStatusBar;
    HBar: TScrollBar;
    View1: TMenuItem;
    LineBrake1: TMenuItem;
    Windows1: TMenuItem;
    Unix1: TMenuItem;
    Fixed1: TMenuItem;
    Hex1: TMenuItem;
    SaveAs1: TMenuItem;
    N1: TMenuItem;
    Cluse1: TMenuItem;
    N2: TMenuItem;
    WordWrap1: TMenuItem;
    procedure FormPaint(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Windows1Click(Sender: TObject);
    procedure Unix1Click(Sender: TObject);
    procedure Fixed1Click(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure Cluse1Click(Sender: TObject);
    procedure WordWrap1Click(Sender: TObject);
    procedure HBarChange(Sender: TObject);
    procedure VBarChange(Sender: TObject);
  private
    { Private declarations }
  public
    FileName    : String;
    FileBuffer  : Pointer;
    FileMemSz   : Cardinal;
    CLRL        : String;
    procedure LoadFile;
  end;

var
  WndViewerMain: TWndViewerMain;

implementation

{$R *.dfm}

procedure TWndViewerMain.FormPaint(Sender: TObject);
var
  i,tH,tW, pZ:Integer;
  Str : String;
  tP : Pointer;
begin
  with Canvas do
  begin
    Brush.Color := clWindow;
    FillRect(GetClientRect);

    Brush.Color := clYellow;
    Pen.Color := clYellow;
    Rectangle(0, 0, 25, ClientHeight);

    tH := TextHeight('8');
    tW := TextWidth('W');

    for i := 0 to (ClientHeight div tH) do
    begin
      TextOut(1, i*tH, IntToStr(i+1));
    end;

    Brush.Color := clWindow;
    i := 0;
    tP := FileBuffer;

    while (Integer(tP) - Integer(FileBuffer)) < FileMemSz do
    begin
      if CLRL <> '' then
      begin
        pZ := Pos(CLRL, PChar(tP))-1;
        if pZ = 0 then
          pZ := FileMemSz;
      end else
      begin
        pZ := (ClientWidth - 27 - VBar.Width) div tW;
      end;

      Str := Copy(PChar(tP), 1, pZ);
      TextOut(27, i, Copy(Str, HBar.Position, Length(Str)));
      i := i + tH;
      tP := Pointer(Integer(tP) + pZ + 2);
      if i > ClientHeight then Break;
    end;
  end;
end;

procedure TWndViewerMain.LoadFile;
var
  f:File of Byte;
begin
  AssignFile(f, FileName);
  Reset(f);

  GetMem(FileBuffer, FileSize(f));
  BlockRead(f, FileBuffer^, FileSize(f));

  FileMemSz := FileSize(f);

  CloseFile(f);
end;

procedure TWndViewerMain.FormCreate(Sender: TObject);
begin
  DoubleBuffered := True;
  CLRL := #13#10;
end;

procedure TWndViewerMain.Windows1Click(Sender: TObject);
begin
  CLRL := #13#10;
  Windows1.Checked := True;
  Repaint;
end;

procedure TWndViewerMain.Unix1Click(Sender: TObject);
begin
  CLRL := #10#13;
  Unix1.Checked := True;
  Repaint;
end;

procedure TWndViewerMain.Fixed1Click(Sender: TObject);
begin
  CLRL := '';
  Fixed1.Checked := True;
  Repaint;
end;

procedure TWndViewerMain.FormResize(Sender: TObject);
begin
  Repaint;
end;

procedure TWndViewerMain.Cluse1Click(Sender: TObject);
begin
  Close;
end;

procedure TWndViewerMain.WordWrap1Click(Sender: TObject);
begin
  WordWrap1.Checked := not WordWrap1.Checked;
  Repaint;
end;

procedure TWndViewerMain.HBarChange(Sender: TObject);
begin
  Repaint;
end;

procedure TWndViewerMain.VBarChange(Sender: TObject);
begin
  Repaint;
end;

end.
